#coding:utf-8
import sys
from PyQt5 import uic
from PyQt5.QtWidgets  import QApplication
from PyQt5.QtWidgets import QMainWindow

class Calc(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.x = None
        self.y = None
        self.result = None
        self.operator = None
        self.equal = None
        
        uic.loadUi('calc.ui',self)
        btns = [        
                self.pushButton0,
                self.pushButton1,
                self.pushButton2,
                self.pushButton3,
                self.pushButton4,
                self.pushButton5,
                self.pushButton6,
                self.pushButton7,
                self.pushButton8,
                self.pushButton9,
                self.pushButton_add,
                self.pushButton_sub,
                self.pushButton_mul,
                self.pushButton_div,
                self.pushButton_equal,
                self.pushButton_dot,
                self.pushButton_clear,
            ]

        map(lambda btn:btn.clicked.connect(self.click_on_button),btns)

    def calculate(self):
        """
        如果x、y、operater不为空，计算结果。计算出结果后，清空y，保持x和结果相等。
        """
        if self.x and self.y and self.operator:
            self.result = str(eval("%s%s%s"%(self.x,self.operator,self.y)))
            self.x = self.result
            self.y = None
            self.text_result.setText(self.result)


    def click_on_button(self):
        value = self.sender().text()
        # 点击数字的处理
        # 1.点击数字时，无运算符且有x，累加x
        # 2.点击数字时，有运算符且有y，累加y
        # 3.点击数字时，无运算符且无x，赋值给x
        # 4.点击数字时，有运算符且无y，赋值给y

        if value in "0123456789.":
            if self.operator is not None:    #有运算符
                if self.y is not None:  #有y
                    self.y = self.y + value
                else:
                    self.y = value
                self.text_result.setText(self.y)

            else:#无运算符
                if self.x is not None:#有x
                    self.x = self.x + value
                else:
                    self.x = value
                self.text_result.setText(self.x)

        # 处理运算符
        # 1.点运算符时记录运算符
        # 2.点运算符时记录运算符并计算结果(x,y,oper都有值)
        # 3.点运算符时记录运算符，并将x置为0(x无值)

        elif value in "+-*/":
            self.operator = value
            if self.x is None:
                self.x = 0
            if self.x and self.operator and self.y is not None:
                self.calculate()

        # 处理点击等号的操作
        # 1.点击等号时计算结果(x,y,oper都有值)
        # 2.点击等号时忽略等号(x,y,oper不都有值)

        elif value == "=":
            if self.x and self.operator and self.y is not None:
                self.calculate()

        else:
            print "你点击的是C"
            self.clear()


    def clear(self):
        self.x = None
        self.y = None
        self.result = None
        self.operator = None
        self.text_result.setText("")

   

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    calc = Calc()
    calc.show()
    app.exec_()


